package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.Book;
import com.ruoyi.system.domain.Txt;
import com.ruoyi.system.domain.ZhyCollect;
import com.ruoyi.system.service.TxtService;
import com.ruoyi.system.service.ZhyCollectService;
import com.ruoyi.web.controller.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class TxtController {
    @Autowired
    private TxtService txtService;
    @Autowired
    private ZhyCollectService zhyCollectService;
    @RequestMapping("/txt/{pageNum}")
    public R<List<Txt>> select(@PathVariable("pageNum") int pageNum)
    {
        PageHelper.startPage(pageNum,6);
        List<Txt> select = txtService.selectall();
        PageInfo<Txt> pageInfo= new PageInfo<>(select);
        return R.success(select.size(),select);
    }

    @PostMapping("/txt/title/{pageNum}")
    public R<List<Txt>> selectName(@PathVariable("pageNum") Integer pageNum,@RequestBody Txt txt)
    {
        PageHelper.startPage(pageNum,6);
        List<Txt> txts = txtService.selectName(txt.getTitle());
        PageInfo<Txt> pageInfo= new PageInfo<>(txts);
         return R.success(txts.size(),txts);
    }
    @PostMapping("/txt/add")
    public R<String> add(@RequestBody Txt txt)
    {
        Txt txt1 = new Txt();
        txt1.setUsername(txt.getUsername());
        txt1.setUserid(txt.getUserid());
        txt1.setClassify(txt.getClassify());
        txt1.setTitle(txt.getTitle());
        txt1.setTxt(txt.getTxt());
        txt1.setStatus(txt.getStatus());
        txtService.add(txt1);
        return R.success(1,"添加完成");
    }
    @RequestMapping("/txt/selectUserid/{pageNum}")
    public R<List<Txt>> selectUserid(@PathVariable Integer pageNum,@RequestBody Txt txt)
    {
        PageHelper.startPage(pageNum,6);
        List<Txt> txts = txtService.selectUserid(txt.getUserid());
        if(txts==null)
        {
            return R.error("查询失败");
        }
        PageInfo<Txt> pageInfo= new PageInfo<>(txts);
        return R.success(txts.size(),txts);
    }
    @RequestMapping("/txt/selectUserid")
    public R<List<Txt>> selectUserids(@RequestBody Txt txt)
    {
        List<Txt> txts = txtService.selectUserid(txt.getUserid());
        if(txts==null)
        {
            return R.error("查询失败");
        }
        return R.success(txts.size(),txts);
    }
    @RequestMapping("/txt/selectId")
    public R<Txt> selectid(@RequestBody Txt txt)
    {
        Txt txt1 = txtService.selectId(txt.getId());
        txt1.setLook(txt1.getLook()+1);
        txtService.look(txt1);
        return R.success(1,txt1);
    }
    @RequestMapping("/txt/delete")
    public R<String> delete(@RequestBody Txt txt)
    {
        txtService.delete(txt.getId());
        ZhyCollect zhyCollect = new ZhyCollect();
        zhyCollect.setType("文章");
        zhyCollect.setId(txt.getId());
        zhyCollectService.deleteCollectById(zhyCollect);
        return R.success(1,"删除成功");
    }
    @RequestMapping("/txt/updateTxt")
    public R<String> updateTxt(@RequestBody Txt txt)
    {
        txtService.updateTxt(txt);
        return R.success(1,"更改成功");
    }
}
